package com.agilestudio.ifinance.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.agilestudio.ifinance.entity.account.Menu;
import com.agilestudio.ifinance.entity.account.User;
import com.agilestudio.ifinance.web.util.Constants;

@Controller
public class HomeController extends AbstractController {

	@RequestMapping("home")
	public String home(Map<String, Object> map) {
		log.info("1===" + "home");
		initMenus(map);
		//log.info("1===" + "home = " + map.get("menus"));
		return "home";
	}

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login(Map<String, Object> map) {
		log.info("1===" + "login");
		return "login";
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public @ResponseBody Map<String, String> login(HttpServletRequest request,HttpServletResponse response,@RequestParam("loginName") String loginName ,@RequestParam("password") String password) {
		log.info("1===" + "login...loginName=" + loginName + " ; password =" + password );
		Map<String, String> map = new HashMap<String, String>();		
		User user = accountService.findUserByLoginName(loginName);
		if( user !=null && user.getPassword().equals(password) )
		{
			HttpSession session = request.getSession(true);
			session.setAttribute(Constants.LOGIN_USER, user);
			user.setLoginTime(new Date());
			accountService.saveUser(user);
			map.put("info", "success");
		}else{
			map.put("info", "fail");
		}
		return map;
	}
	
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		log.info("Logout = " + request.getSession().getId());		
		request.getSession().invalidate();
		return "login";
	}	

	@RequestMapping("tree")
	public @ResponseBody
	List<Map<String, ? extends Object>> tree() {
		log.info("1====" + "tree...");
		List<Menu> allMenus = accountService.getMenus();
		List<Map<String, ? extends Object>> treeList = constructTree("",
				allMenus);
		return treeList;
	}

	@RequestMapping("test")
	public String test() {
		log.info("#############test start ############");
		return "test";
	}

	private List<Map<String, ? extends Object>> constructTree(String code,
			List<Menu> allMenus) {
		List<Map<String, ? extends Object>> atree = new ArrayList<Map<String, ? extends Object>>();
		List<Menu> menus = accountService.getMenus(code, allMenus);
		for (Menu menu : menus) {
			Map<String, Object> node = new HashMap<String, Object>();
			Map<String, Object> data = new HashMap<String, Object>();
			Map<String, Object> metadata = new HashMap<String, Object>();
			Map<String, Object> attr = new HashMap<String, Object>();
			// child.put("id", menu.getId());
			// data.put("icon", menu.isLeaf() ? "file" : "folder");
			data.put("title", menu.getName());
			node.put("data", data);
			// metadata.put("id", menu.getId());
			// metadata.put("code", menu.getCode());
			// node.put("metadata", metadata);
			attr.put("id", menu.getId());
			attr.put("href", menu.getUrl());
			attr.put("target", "right");
			attr.put("leaf", menu.isLeaf());
			node.put("attr", attr);
			List<Map<String, ? extends Object>> x = constructTree(
					menu.getCode(), allMenus);
			if (x != null && x.size() > 0) {
				node.put("children", x);
			}
			atree.add(node);
		}
		return atree;
	}
}
